package com.hghivln.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hghivln.constants.Constants;
import com.hghivln.mapper.ProducterMapper;
import com.hghivln.pojo.domain.Producter;
import com.hghivln.pojo.dto.ProducterDto;
import com.hghivln.pojo.vo.AjaxResult;
import com.hghivln.service.ProducterService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@Service
public class ProducterServiceImpl implements ProducterService {

    @Autowired
    private ProducterMapper producterMapper;

    @Override
    public AjaxResult listProducterForPage(ProducterDto producterDto) {
        Page<Producter> page = new Page<>(producterDto.getPageNum(), producterDto.getPageSize());
        QueryWrapper<Producter> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(producterDto.getProducterName()),Producter.COL_PRODUCTER_NAME,producterDto.getProducterName());
        qw.like(StringUtils.isNotBlank(producterDto.getKeywords()),Producter.COL_KEYWORDS,producterDto.getKeywords());
        qw.like(StringUtils.isNotBlank(producterDto.getProducterTel()),Producter.COL_PRODUCTER_TEL,producterDto.getProducterTel());
        qw.eq(StringUtils.isNotBlank(producterDto.getStatus()),Producter.COL_STATUS,producterDto.getStatus());
        qw.ge(producterDto.getBeginTime() != null,Producter.COL_CREATE_TIME,producterDto.getBeginTime());
        qw.le(producterDto.getEndTime() != null,Producter.COL_CREATE_TIME,producterDto.getEndTime());
        qw.eq(Producter.COL_DEL_FLAG, Constants.DEL_FALSE);
        qw.orderByAsc(Producter.COL_CREATE_TIME);
        producterMapper.selectPage(page, qw);
        return AjaxResult.success("查询成功", page.getRecords(), page.getTotal());
    }

    @Override
    public AjaxResult addProducter(ProducterDto producterDto) {
        Producter producter = new Producter();
        BeanUtils.copyProperties(producterDto, producter);
        producter.setCreateBy(producterDto.getSimpleUser().getUserName());
        producter.setCreateTime(DateUtil.date());
        return AjaxResult.success(producterMapper.insert(producter));
    }

    @Override
    public AjaxResult updateProducter(ProducterDto producterDto) {
        Producter producter = new Producter();
        BeanUtils.copyProperties(producterDto, producter);
        producter.setUpdateBy(producterDto.getSimpleUser().getUserName());
        return AjaxResult.success(producterMapper.updateById(producter));
    }

    @Override
    public AjaxResult deleteProducterByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        if (idList.size() > 0) {
            return AjaxResult.success(producterMapper.deleteProducterByIds(idList));
        }
        return AjaxResult.fail("删除失败");
    }

    @Override
    public AjaxResult selectProducterById(Long id) {
        QueryWrapper<Producter> qw = new QueryWrapper<>();
        qw.eq(Producter.COL_DEL_FLAG, Constants.DEL_FALSE);
        qw.eq(Producter.COL_PRODUCTER_ID, id);
        return AjaxResult.success(producterMapper.selectOne(qw));
    }

    @Override
    public AjaxResult selectAllProducter() {
        QueryWrapper<Producter> qw = new QueryWrapper<>();
        qw.eq(Producter.COL_DEL_FLAG, Constants.DEL_FALSE);
        qw.eq(Producter.COL_STATUS, Constants.STATUS_TRUE);
        return AjaxResult.success(producterMapper.selectList(qw));
    }

}

